/*
$.ajax({
	url:url,
	type : 'POST',
	data : '',
	async :true,
	dataType :'json',
    beforeSend: function(){  
        $.messager.progress() 
    },
    complete: function(){  
        $.messager.progress('close'); 
    },
    success : function(result) {
    	if(result.success){
			gd.datagrid('reload');
		} else {  
        	$.messager.show({ title: 'Error',msg: result.msg }); 
        }
    }
});*/
/** *************************公用方法*************************** */
// Add and Edit
function gridAddAndEdit(title, url, type, grid, fm, dlg , dlgUrl) {
	//alert(title+","+ url+","+ type+","+ grid+","+ fm+","+ dlg+","+dlgUrl);
	var gd=$('#' + grid);
	if (type == 1) { // edit
		var rows = gd.datagrid('getSelections');
		if (rows.length != 1) {
			$.messager.alert('消息', '请钩择一行数据!', 'info');
			return;
		}
	}
	$('<div/>').dialog({
		href : dlgUrl,
		modal : true,
		title : title,
		top : '15%',
		left : '30%',
		width : 600,
		resizable : true,
		buttons : [ {
			text : '确定',
			iconCls : 'icon-ok',
			handler : function() {
				$.messager.progress();
				$('#' + fm).form('submit', {
				    url : url,
					onSubmit: function(){
						var isValid = $(this).form('validate');
						if (!isValid){
							$.messager.progress('close');	
						}
						return isValid;	
					},
					success : function(data) {
						$.messager.progress('close');
						var result=eval('('+data+')');
						if(result.success){
							$('#' + dlg).dialog('close');
							gd.datagrid('reload');
						} else {  
				        	$.messager.show({ title: 'Error',msg: result.msg }); 
				        }  
					}
				});
				//关闭清除
				$(this).closest('.window-body').dialog('destroy');
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			if (type == 0) {
				$('#' + fm).form('clear');
			} else {
				var rows = $('#' + grid).datagrid('getSelections');
				if (rows.length == 1) {
					$('#' + fm).form('load', rows[0]);
				} else {
					$.messager.alert('消息', '请钩择一行数据!', 'info');
				}
			}
		}
	});
}
// Search
function search(fm, grid, type) {
	var gd = $('#' + grid);
	gd.datagrid('options').pageNumber = 1;
	var obj = $('#' + fm).serializeJson();
	obj['searchType'] = type;
	gd.datagrid({
		queryParams : obj
	});
}
// clear Reset
function formMethod(fm, method) {
	$('#' + fm).form(method);
}
// Reload
function gridReload(grid) {
	var gd = $('#' + grid);
	gd.datagrid('options').pageNumber = 1;
	gd.datagrid('load', {});
}
//Delete
function gridDelete(grid, url) {
	var gd = $('#' + grid);
	var rows = gd.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('Confirm', '确定要删除选择的数据吗?', function(r) {
			if (r) {
				var ids = '';
				for ( var i = 0; i < rows.length; i++) {
					ids += 'ids=' + rows[i].id + '&';
				}
				ids = ids.substring(0, ids.length - 1);
				$.get(url + "?" + ids, function(result) {
					if (result.success) {
						gd.datagrid('reload');
						gd.datagrid('clearSelections');
					} else {
						$.messager.show({
							title : 'Error',
							msg : result.msg
						});
					}
				}, 'json');
			}
		});
	} else {
		$.messager.alert('消息', '请选择要删除的数据!', 'info');
	}
}
//Export 导出
function gridExportAndImport(url,type){
	if(type==0){  //Export 导出
		window.location=url;
		return;
	}
	if(type==1){  // Import 导入
		$('<div/>').dialog({
			href : url,
			modal : true,
			title : '导入',
			top : '15%',
			left : '30%',
			width : 600,
			height: 300,
			resizable:true,
			buttons : [ {
				text : '确定',
				iconCls : 'icon-ok',
				handler : function() {
					//$(this).closest('.window-body').dialog('destroy');
				}
			}, {
				text : '取消',
				iconCls : 'icon-cancel',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				
			}
		});
		return;
	}
}

//高级搜索  Delete line row
function searchARemove(curr) {
	if ($(curr).closest('table').find('tr').size() > 2) {
		$(curr).closest('tr').remove();
	} else {
		alert('该行不允许删除');
	}
}

//高级查询
function searchA(grid,fm,url) {
	var gd = $('#' + grid);
	$('<div/>').dialog({
		href : url,
		modal : true,
		title : '高级查询',
		top : 120,
		width : 480,
		buttons : [ {
			text : '增加一行',
			iconCls : 'icon-add',
			handler : function() {
				var currObj = $(this).closest('.panel').find('table');
				currObj.find('tr:last').clone().appendTo(currObj);
				//currObj.find('tr:last td *[disabled]').removeAttr("disabled");
			}
		}, {
			text : '确定',
			iconCls : 'icon-ok',
			handler : function() {
				gd.datagrid('options').pageNumber=1;
				gd.datagrid('load',serializeObjectEx($('#' + fm)));
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});
}

//样式切换
$(function(){
        var themeDiv=$("#styleMenu div div").click(function(){
            var themeName=$.trim($(this).html());
            var $easyuiTheme = $('#easyuiTheme');
			var url = $easyuiTheme.attr('href');
			var href = url.substring(0, url.indexOf('themes')) + 'themes/' + themeName + '/easyui.css';
			$easyuiTheme.attr('href', href);
			/*
			var $iframe = $('iframe');
			if ($iframe.length > 0) {
				for ( var i = 0; i < $iframe.length; i++) {
					var ifr = $iframe[i];
					$(ifr).contents().find('#easyuiTheme').attr('href', href);
				}
			}
			*/
			$.cookie('easyuiThemeName', themeName, {
				expires : 7
			});
        });
});

serializeObject = function(form) {
	var o = {};
	$.each(form.serializeArray(), function(index) {
		if (o[this['name']]) {
			o[this['name']] = o[this['name']] + "," + this['value'];
		} else {
			o[this['name']] = this['value'];
		}
	});
	return o;
};

serializeObjectEx = function(form) {
	//console.dir(form.serializeArray());
	var o = {};
	$.each(form.serializeArray(), function(index) {
		if (o[this['name']]) {
			o[this['name']] = o[this['name']] + "," + (this['value']==''?' ':this['value']);
		} else {
			o[this['name']] = this['value']==''?' ':this['value'];
		}
	});
	//console.dir(o);
	return o;
};

/**
 * 
 * 改变jQuery的AJAX默认属性和方法
 */
$.ajaxSetup({
	type : 'POST',
	error : function(XMLHttpRequest, textStatus, errorThrown) {
		$.messager.progress('close');
		$.messager.alert('错误', XMLHttpRequest.responseText);
	}
});

//默认配置
if($.fn.panel){
	$.fn.panel.defaults.loadingMessage = '加载中....';
}
if ($.fn.pagination){
	$.fn.pagination.defaults.beforePageText = '第';
	$.fn.pagination.defaults.afterPageText = '共{pages}页';
	$.fn.pagination.defaults.displayMsg = '显示{from}到{to},共{total}条';//记录
}
if ($.fn.datagrid){
	$.fn.datagrid.defaults.loadMsg = '正在处理，请稍待。。。';
	//
	$.fn.datagrid.defaults.pagination = true;
	$.fn.datagrid.defaults.rownumbers=true;
	$.fn.datagrid.defaults.multiple=true;
	//$.fn.datagrid.defaults.nowrap=false;
	$.fn.datagrid.defaults.idField='id';
}
if ($.fn.treegrid && $.fn.datagrid){
	$.fn.treegrid.defaults.loadMsg = $.fn.datagrid.defaults.loadMsg;
	//
	$.fn.treegrid.defaults.pagination = true;
	$.fn.treegrid.defaults.rownumbers=true;
	//$.fn.treegrid.defaults.nowrap=false;
	//$.fn.treegrid.defaults.multiple=true;
	$.fn.treegrid.defaults.idField='id';
	$.fn.treegrid.defaults.treeField='name';
}
if ($.messager){
	$.messager.defaults.ok = '确定';
	$.messager.defaults.cancel = '取消';
}
if ($.fn.validatebox){
	$.fn.validatebox.defaults.missingMessage = '该输入项为必输项';
	$.fn.validatebox.defaults.rules.email.message = '请输入有效的电子邮件地址';
	$.fn.validatebox.defaults.rules.url.message = '请输入有效的URL地址';
	$.fn.validatebox.defaults.rules.length.message = '输入内容长度必须介于{0}和{1}之间';
	$.fn.validatebox.defaults.rules.remote.message = '请修正该字段';
}
if ($.fn.numberbox){
	$.fn.numberbox.defaults.missingMessage = '该输入项为必输项';
}
if ($.fn.combobox){
	$.fn.combobox.defaults.missingMessage = '该输入项为必输项';
	//
	$.fn.combo.defaults.multiple=true;
}
if ($.fn.combotree){
	$.fn.combotree.defaults.missingMessage = '该输入项为必输项';
	//
	$.fn.combotree.defaults.multiple=true;
	$.fn.combotree.defaults.lines=true;
}
if ($.fn.combogrid){
	$.fn.combogrid.defaults.missingMessage = '该输入项为必输项';
	//
	$.fn.combogrid.defaults.pagination = true;
	$.fn.combogrid.defaults.rownumbers=true;
	$.fn.combogrid.defaults.multiple=true;
	$.fn.combogrid.defaults.nowrap=false;
	$.fn.combogrid.defaults.idField='id';
}
if ($.fn.calendar){
	$.fn.calendar.defaults.weeks = ['日','一','二','三','四','五','六'];
	$.fn.calendar.defaults.months = ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'];
}
if ($.fn.datebox){
	$.fn.datebox.defaults.currentText = '今天';
	$.fn.datebox.defaults.closeText = '关闭';
	$.fn.datebox.defaults.okText = '确定';
	$.fn.datebox.defaults.missingMessage = '该输入项为必输项';
	$.fn.datebox.defaults.formatter = function(date){
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
	};
	$.fn.datebox.defaults.parser = function(s){
		if (!s) return new Date();
		var ss = s.split('-');
		var y = parseInt(ss[0],10);
		var m = parseInt(ss[1],10);
		var d = parseInt(ss[2],10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
			return new Date(y,m-1,d);
		} else {
			return new Date();
		}
	};
}
if ($.fn.datetimebox && $.fn.datebox){
	$.extend($.fn.datetimebox.defaults,{
		currentText: $.fn.datebox.defaults.currentText,
		closeText: $.fn.datebox.defaults.closeText,
		okText: $.fn.datebox.defaults.okText,
		missingMessage: $.fn.datebox.defaults.missingMessage
	});
	
	$.fn.datetimebox.defaults.required=false;
	$.fn.datetimebox.defaults.showSeconds=true;
}
